﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Mate.Core.Base;
using Mate.Core.Configuration;
using Mate.Framework.Extensions;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Mvc;

namespace Mate.Admin.Controllers.System
{
    public class UploadController : Controller
    {
        private readonly IHostingEnvironment environment;

        private readonly MateConfig mateConfig;
        public UploadController(IHostingEnvironment environment, MateConfig mateConfig)
        {
            this.environment = environment;
            this.mateConfig = mateConfig;
        }

        #region 上传图片
        [HttpPost]
        public JsonResult UpdateImage(string imagePath)
        {
          
            var files = Request.Form.Files["pictureFile"];
           
            string zoomPath;
            var pictureResult = files.Picture(environment.WebRootPath,imagePath, out zoomPath);
            var result = new BaseResult<object>();
            if (pictureResult.ResultCode==ResultCode.Success)
            {

                result = new BaseResult<object>()
                {
                    ResultCode=ResultCode.Success,
                    Message=pictureResult.Message,
                    Data= new
                    {
                        ImageUrl = pictureResult.Data,
                        ThumImageUrl = zoomPath
                    }
                };
                return Json(result);
            }
            else
            {
                return Json(pictureResult);
            }
        }

        [HttpPost]
        public JsonResult UpdateImageNotZoom(string imagePath)
        {
            var files = Request.Form.Files["pictureFile"];
            var pictureResult = mateConfig.QiNiu.QiniuUpload(files);
        
            //var pictureResult = files.Picture(environment.WebRootPath, imagePath);
            var result = new BaseResult<object>();
            if (pictureResult.ResultCode == ResultCode.Success)
            {

                result = new BaseResult<object>()
                {
                    ResultCode = ResultCode.Success,
                    Message = pictureResult.Message,
                    Data = new
                    {
                        ImageUrl = pictureResult.Data,
                    }
                };
                return Json(result);
            }
            else
            {
                return Json(pictureResult);
            }
        }
        #endregion

        #region 上传文件
        //[HttpPost]
        //public JsonResult UpdateFile(string filePath)
        //{

        //    var files = Request.Files["uploadFile"];
        //    var fileResult = files.File(filePath);
        //    var result = new BaseResult<object>();
        //    if (fileResult.IsSuccess)
        //    {
        //        result = new BaseResult<object>(fileResult.IsSuccess,
        //            new
        //            {
        //                FileName = files.FileName,
        //                FileUrl = fileResult.Data
        //            });
        //        return Json(result);
        //    }
        //    else
        //    {
        //        return Json(fileResult);
        //    }
        //}
        #endregion
    }
}